<?php
include("config.php");
include("user.class.php");

if($_GET['request']==1){
	if(validateEmail($_GET[user_mail])){
		$results=mysql_query("select * from `user` where user_email='$_GET[user_mail]'")or die(mysql_error());
		$count=mysql_num_rows($results);
		if($count!=0){echo "用户已存在";}else{echo "true";}
	}else{echo "邮箱格式错误";}
}

if($_GET['request']==2){
	if($_SESSION['check']==$_GET['yzm'])echo "true";
	}
	
if($_GET['request']==3){
	$pwd=md5($_GET['user_pwd']);
	$re=mysql_query("select u_id from user where user_email='administrator'");
	$row=mysql_fetch_array($re);
	$u_id=$row['u_id'];
	$new_u_id=$u_id+1;
	$x=mysql_query("update user set u_id='$new_u_id' where user_email='administrator'");
	$x=mysql_query("insert into user (id,user_email,user_pwd,u_id,create_time)values(null,'$_GET[user_mail]','$pwd','$u_id',now())");
	if($x){
		$_SESSION['login_mail']=$_GET['user_mail'];
		imageStorage("img/simple.png");
		echo "true";
		}
	else{echo mysql_error();}
	}

//图片入库
function imageStorage($picture){
	if(file_exists($picture)){
		$PSize = filesize($picture);
  	$mysqlPicture = addslashes(fread(fopen($picture,"r"), $PSize));
  	$user=$_SESSION['login_mail'];
		mysql_query("update user set `avatar`='$mysqlPicture' where user_email='$user'") or die(mysql_error());
 	} 
}


function validateEmail($email){
	 $isValid = true;
   $atIndex = strrpos($email,"@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
//      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
//      {
//         // domain not found in DNS
//         $isValid = false;
//      }
   }
   return $isValid;
	}
?>